package com.tt.fashionWebSite.controller;

import com.tt.fashionWebSite.dto.ChiTietSanPhamDto;
import com.tt.fashionWebSite.dto.ErrorCode;
import com.tt.fashionWebSite.dto.IndexTestDto;
import com.tt.fashionWebSite.dto.JsonResponseDto;
import com.tt.fashionWebSite.service.IndexTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 10520_000 on 10/23/14.
 */
@Controller
public class IndexTestController {
    @Autowired
    private IndexTestService indexTestService;

    @SuppressWarnings("rawtypes")
    @RequestMapping(value = "/indexTest/test", method = RequestMethod.POST)
    public @ResponseBody JsonResponseDto<MultipartFile> emailContact(@RequestParam("myfile") MultipartFile files, @RequestParam("maCTSP") String maCTSP) {
        try {
            byte[] bytes = files.getBytes();
            Date now = new Date();
            // Creating the directory to store file
            String rootPath = System.getProperty("catalina.home");
            File dir = new File(rootPath + File.separator + "tmpFiles");
            if (!dir.exists())
                dir.mkdirs();

            // Create the file on server
            File serverFile = new File(dir.getAbsolutePath()
                  + File.separator + now.getTime() + files.getOriginalFilename());
            BufferedOutputStream stream = new BufferedOutputStream(
                  new FileOutputStream(serverFile));
            stream.write(bytes);
            stream.close();
        }
        catch (Exception ex) {
           System.out.print(ex);
        }
        return new JsonResponseDto<MultipartFile>( ErrorCode.SUCCESS);
    }

//    @SuppressWarnings("rawtypes")
//    @RequestMapping(value = "/indexTest/test", method = RequestMethod.POST)
//    public @ResponseBody JsonResponseDto<List<IndexTestDto>> emailContact(@RequestParam("file") MultipartFile[] files) {
//        return new JsonResponseDto<List<IndexTestDto>>( ErrorCode.SUCCESS);
//    }
}
